package role

import (
	"context"
	"fmt"
	"net/http"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MenuListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMenuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MenuListLogic {
	return &MenuListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MenuListLogic) MenuList(req *types.RoleRequest) (resp *types.RoleMenusResponse, err error) {
	resp = new(types.RoleMenusResponse)

	//1.查询角色分配的菜单列表
	list, err := l.svcCtx.RoleMenuModel.FindByPage(l.ctx, fmt.Sprintf(" where role_id=%d", req.Id))
	if err != nil {
		fmt.Printf("[Error]查询角色[%d]菜单列表:%s\n", req.Id, err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	for _, one := range list {
		resp.Data = append(resp.Data, one.MenuId)
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"

	return resp, nil
}
